<?php
if (!class_exists("ShortList")) include_once dirname(__FILE__).'/ShortList.class.php';
if (!class_exists("ImageLink"))include_once dirname(__FILE__).'/ImageLink.class.php';
if (!class_exists("TemplatePatterns")) include_once dirname(__FILE__).'/TemplatePatterns.class.php';
if (!class_exists("URLVariables")) include_once dirname(__FILE__).'/../request/URLVariables.class.php';

class ListItems {
	private $_itemRender;
	private $_listSources;
	private $REPEAT_DELIMER = "|";
	private $_shortListController;
	private $_keyVars;
	
	const NUMBER 			= "#";
	const NUMBER_NOT 		= "!#";
	const ACTION 			= "action"; 
	const ID 				= "id"; 
	const TO 				= "to";
	
	const ADD 				= "_add";
	const EDIT 				= "_edit";
	const DELETE 			= "_delete";
	const MOVE 				= "_move";
	const MOVE_UP 			= "_up";
	const MOVE_DOWN 		= "_down";
	
	function __construct(ShortList &$shortListController, $keyVars = ""){
		$this->_shortListController = $shortListController;
		$this->_keyVars = $keyVars;
	}
	
	public function draw($itemRender = ""){
		if ($itemRender != "")
			$this->_itemRender = $itemRender;
		$itemsArray = array();
		preg_match_all(TemplatePatterns::tvar, $this->_itemRender, $itemsArray);		
		foreach ($this->_listSources as $key => $item){
			$string = $this->_itemRender;
			foreach($itemsArray[1] as $k => $v){
				if (strpos($v, $this->REPEAT_DELIMER) > 0){
					$repeat = preg_split("/\\$this->REPEAT_DELIMER/", $v);
					$countRepeat = count($repeat);
					$keyRepeat = $key - $countRepeat * floor($key/$countRepeat);
					$string = str_replace($itemsArray[0][$k], $repeat[$keyRepeat], $string);
				} else 
				switch ($v){
					case self::NUMBER: 
						$countOnPage = $this->_shortListController->countOnPage();
						$currentPage = $this->_shortListController->getCurrentPage();
						$number = $currentPage * $countOnPage + $key +1;
						$string = str_replace($itemsArray[0][$k], $number, $string);
					break;
					
					case self::NUMBER_NOT:
						$countOnPage = $this->_shortListController->countOnPage();
						$currentPage = $this->_shortListController->getCurrentPage();
						$number = $this->_shortListController->getAllCount() - ($currentPage * $countOnPage + $key);
						$string = str_replace($itemsArray[0][$k], $number, $string);
					break;
					
					case self::EDIT:
						$qs = new URLVariables();
						$qs->addVar($this->_keyVars.self::ACTION, self::EDIT);
						$qs->addVar($this->_keyVars.self::ID, $item[self::ID]);
						$string = str_replace($itemsArray[0][$k], $qs->getString(), $string);
					break;
					
					case self::DELETE:
						$qs = new URLVariables();
						$qs->addVar($this->_keyVars.self::ACTION, self::DELETE);
						$qs->addVar($this->_keyVars.self::ID, $item[self::ID]);
						$string = str_replace($itemsArray[0][$k], $qs->getString(), $string);
					break;
					
					case self::MOVE_UP:
						$qs = new URLVariables();
						$qs->addVar($this->_keyVars.self::ACTION, self::MOVE);
						$qs->addVar($this->_keyVars.self::ID, $item[self::ID]);
						$qs->addVar(self::TO, 1);
						$string = str_replace($itemsArray[0][$k], $qs->getString(), $string);
					break;
					
					case self::MOVE_DOWN:
						$qs = new URLVariables();
						$qs->addVar($this->_keyVars.self::ACTION, self::MOVE);
						$qs->addVar($this->_keyVars.self::ID, $item[self::ID]);
						$qs->addVar(self::TO, -1);
						$string = str_replace($itemsArray[0][$k], $qs->getString(), $string);
					break;
					
					default:
						if (isset($item[$v]))
							$string = str_replace($itemsArray[0][$k], $item[$v], $string);
					break;
				}			
			}
			echo $string;
		}
	}	
	public function drawNavigation($itemRender){
		$array = $this->_shortListController->draw($itemRender);
	}
	
	public function setListSources(array $list){
		$this->_listSources = $list;
	}
	
	public function getListSources(){
		return $this->_listSources;
	}
	
	public function setItemRender($templateString){
		$this->_itemRender = $templateString;
	}
	
	public function getItemRender(){
		return $this->_itemRender;
	}
	
	public function getkeyVars(){
		return $this->_keyVars;
	}
}
?>